package com.youlai.boot.modules.tp.controller;

import com.youlai.boot.common.result.Result;
import com.youlai.boot.modules.tp.dto.query.DateRangeQuery;
import com.youlai.boot.modules.tp.dto.response.SupplyChainDashboardVO;
import com.youlai.boot.modules.tp.service.SupplyChainDashboardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 供应链看板控制器
 *
 * @author Jason
 * @since 2025-06-04
 */
@Tag(name = "供应链看板")
@RestController
@RequestMapping("/api/v1/supply-chain/dashboard")
@RequiredArgsConstructor
public class SupplyChainDashboardController {

    private final SupplyChainDashboardService supplyChainDashboardService;

    @Operation(summary = "获取供应链看板完整数据")
    @GetMapping("/all")
    public Result<SupplyChainDashboardVO> getAllDashboardData(
            @Parameter(description = "开始日期") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @Parameter(description = "结束日期") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        
        DateRangeQuery dateRange = DateRangeQuery.builder()
                .startDate(startDate != null ? startDate : LocalDate.now().minusDays(30))
                .endDate(endDate != null ? endDate : LocalDate.now())
                .build();
        
        SupplyChainDashboardVO dashboardData = supplyChainDashboardService.getDashboardData(dateRange);
        return Result.success(dashboardData);
    }

    @Operation(summary = "获取概览指标")
    @GetMapping("/overview")
    public Result<SupplyChainDashboardVO.OverviewMetrics> getOverviewMetrics(
            @Parameter(description = "报告日期") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate reportDate) {
        
        LocalDate date = reportDate != null ? reportDate : LocalDate.now();
        SupplyChainDashboardVO.OverviewMetrics overview = supplyChainDashboardService.getOverviewMetrics(date);
        return Result.success(overview);
    }

    @Operation(summary = "获取库存分析数据")
    @GetMapping("/inventory-analysis")
    public Result<SupplyChainDashboardVO.InventoryAnalysis> getInventoryAnalysis(
            @Parameter(description = "报告日期") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate reportDate) {
        
        LocalDate date = reportDate != null ? reportDate : LocalDate.now();
        SupplyChainDashboardVO.InventoryAnalysis analysis = supplyChainDashboardService.getInventoryAnalysis(date);
        return Result.success(analysis);
    }

    @Operation(summary = "获取周转分析数据")
    @GetMapping("/turnover-analysis")
    public Result<SupplyChainDashboardVO.TurnoverAnalysis> getTurnoverAnalysis(
            @Parameter(description = "开始日期") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @Parameter(description = "结束日期") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        
        DateRangeQuery dateRange = DateRangeQuery.builder()
                .startDate(startDate != null ? startDate : LocalDate.now().minusDays(30))
                .endDate(endDate != null ? endDate : LocalDate.now())
                .build();
        
        SupplyChainDashboardVO.TurnoverAnalysis analysis = supplyChainDashboardService.getTurnoverAnalysis(dateRange);
        return Result.success(analysis);
    }

    @Operation(summary = "获取目标达成数据")
    @GetMapping("/target-achievement")
    public Result<SupplyChainDashboardVO.TargetAchievement> getTargetAchievement(
            @Parameter(description = "报告日期") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate reportDate) {
        
        LocalDate date = reportDate != null ? reportDate : LocalDate.now();
        SupplyChainDashboardVO.TargetAchievement achievement = supplyChainDashboardService.getTargetAchievement(date);
        return Result.success(achievement);
    }

    @Operation(summary = "获取店铺表现排行")
    @GetMapping("/store-performance")
    public Result<List<SupplyChainDashboardVO.StorePerformance>> getStorePerformance(
            @Parameter(description = "报告日期") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate reportDate,
            @Parameter(description = "排序字段") @RequestParam(defaultValue = "achievementRate") String sortBy,
            @Parameter(description = "数量限制") @RequestParam(defaultValue = "10") Integer limit) {
        
        LocalDate date = reportDate != null ? reportDate : LocalDate.now();
        List<SupplyChainDashboardVO.StorePerformance> performances = 
                supplyChainDashboardService.getStorePerformance(date, sortBy, limit);
        return Result.success(performances);
    }

    @Operation(summary = "获取库存预警信息")
    @GetMapping("/inventory-alerts")
    public Result<List<SupplyChainDashboardVO.InventoryAlert>> getInventoryAlerts(
            @Parameter(description = "报告日期") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate reportDate,
            @Parameter(description = "预警级别") @RequestParam(required = false) String alertLevel) {
        
        LocalDate date = reportDate != null ? reportDate : LocalDate.now();
        List<SupplyChainDashboardVO.InventoryAlert> alerts = 
                supplyChainDashboardService.getInventoryAlerts(date, alertLevel);
        return Result.success(alerts);
    }

    @Operation(summary = "获取趋势数据")
    @GetMapping("/trend")
    public Result<SupplyChainDashboardVO.TrendData> getTrendData(
            @Parameter(description = "开始日期") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @Parameter(description = "结束日期") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
            @Parameter(description = "趋势类型") @RequestParam(defaultValue = "all") String trendType) {
        
        DateRangeQuery dateRange = DateRangeQuery.builder()
                .startDate(startDate != null ? startDate : LocalDate.now().minusDays(30))
                .endDate(endDate != null ? endDate : LocalDate.now())
                .build();
        
        SupplyChainDashboardVO.TrendData trendData = supplyChainDashboardService.getTrendData(dateRange, trendType);
        return Result.success(trendData);
    }

    @Operation(summary = "刷新供应链数据")
    @PostMapping("/refresh")
    public Result<String> refreshSupplyChainData() {
        supplyChainDashboardService.refreshData();
        return Result.success("数据刷新成功");
    }
} 